<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2019/2/13
 * Time: 17:12
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;

class BatchMemberParameter extends BaseValidate
{
    protected $rule=[
        'type'=>'require|checkType',
        'info'=>'require|checkInfo',
        'list'=>'require|checkList',
    ];


    public function checkType($value){
        if ($value =='on' || $value=='off'){
            return true;
        }
        throw new ParameterException([
            'msg'=>'type参数不正确'
        ]);
    }

    public function checkInfo($value){
        if ($value =='ai' || $value=='boss'){
            return true;
        }
        throw new ParameterException([
            'msg'=>'info参数不正确'
        ]);

    }

    /**
     * 检查list参数是否合法
     * @param $value
     * @return bool
     * @throws ParameterException
     */
    public function checkList($value){
        if (!is_array($value)){
            throw new ParameterException([
               'list'=>'list参数不正确'
            ]);
        }
        foreach ($value as $item){
            if (!$this->isPositiveInt($item)){
                throw new ParameterException([
                    'list'=>'list中每个子项必须为正整数'
                ]);
            }
        }
        return true;
    }
}